package com.cfc.http.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;


import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class WordUtil {
    public static void simpleaord(Map<String,String> dataMap) throws Exception {
        // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
        /*Map<String,String> dataMap = new HashMap<String,String>();
        dataMap.put("name", "张三");*/
 /*       File file = new File("E:\\aa\\a.jpg");
        InputStream is = new FileInputStream(file);
        byte[] data = new byte[is.available()];
        is.read();
        is.close();
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(data);
        System.out.println(encode);*/

        dataMap.put("image",getImageStr("E:\\aa\\a.jpg"));
        /*dataMap.put("price", "20000");
        dataMap.put("name", "法拉利");
        dataMap.put("num", "2");*/

        //Configuration用于读取ftl文件
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");

        /*以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是
         * 指定ftl文件所在目录的路径,而不是ftl文件的路径
         */
        //指定路径的第一种方式(根据某个类的相对路径指定)
        //configuration.setClassForTemplateLoading(this.getClass(),"");

        //指定路径的第二种方式,我的路径是C:/a.ftl
        try {
            configuration.setDirectoryForTemplateLoading(new File("E:/aa"));
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 输出文档路径及名称
        File outFile = new File("D:/贷款合同.doc");

        //以utf-8的编码读取ftl文件
        Template t = null;
        try {
            t = configuration.getTemplate("d.ftl","utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            t.process(dataMap, out);
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String getImageStr(String imgFile) {
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
}
